using System;
using System.Text;
using System.IO.Ports;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using System.Net;

namespace ParasiTracer
{
    internal sealed class TracerIO
    {
        #region SINGLETON
        private class Nested
        {
            internal static readonly TracerIO instance = new TracerIO();

            static Nested()
            {
                //
            }
        }
        public static TracerIO inst
        {
            get
            {
                return Nested.instance;
            }
        }
        #endregion

        static object _writerLock = new object();
        static SerialPort _sp;

        TracerIO ()
        {
            try
            {
                Logger.inst.logInfo("TracerIO::.ctor");
                Logger.inst.logInfo("Iniciando serialport...");
                _sp = new SerialPort(
                    Config.inst.serial_port,
                    Config.inst.serial_baud_rate
                );
                _sp.Handshake = (Handshake)Config.inst.serial_hand_shake;
                _sp.Open();
                Logger.inst.logInfo(
                    "Serialport iniciado: {0} {1} {2}",
                    Config.inst.serial_port,
                    Config.inst.serial_baud_rate,
                    (Handshake)Config.inst.serial_hand_shake
                );
            }
            catch (Exception exx)
            {
                int foo = 0;
            }
        }

        public void write(string s)
        {
            lock (_writerLock)
            {
                _sp.Write(s);
            }
        }
    }
}
